<template>
  <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <el-table border v-loading="loading" :data="dataList" ref="table" show-summary :summary-method="getSummaries"
                :max-height="tableMaxHeight">
        <el-table-column label="请购单号" align="center" prop="nos"/>
        <el-table-column label="产品编码" align="center" prop="goodsCode"/>
        <el-table-column label="产品名称" align="center" prop="goodsName"/>
        <el-table-column label="产品型号" align="center" prop="goodModel"/>
        <el-table-column label="产品单位" align="center" prop="unit"/>
        <el-table-column label="请购数量" align="center" prop="shengyu"/>
        <el-table-column label="重量" align="center" prop="dun"/>
        <el-table-column label="中标价" align="center" prop="goodsUnitPrice"/>
        <el-table-column label="采购单价" align="center" prop="goodsBuyPrice"/>
        <el-table-column label="采购总价" align="center" prop="goodsBuyTotal"/>
        <el-table-column label="需求日期" align="center" prop="saleDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务员" align="center" prop="salesmanName"/>
        <el-table-column label="工程名称" align="center" prop="projectName"/>
        <el-table-column label="客户名称" align="center" prop="customerName"/>
        <el-table-column label="供应商" align="center" prop="supplierName"/>
        <el-table-column label="备注" align="center" prop="remark"/>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, getAction} from '@/api/manage'

export default {
  name: "GoodsSalebuyLists",
  components: {},
  data() {
    return {
      title: "记录",
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售信息管理表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        cgallid: null,
        pageNum: 1,
        pageSize: 20,
        mainId: null,
        name: null,
        brand: null,
        unit: null,
        costPrice: null,
        count: null,
        cost: null,
        validityTerm: null,
        buyId: null,
        buyName: null,
        buyDate: null,
        buyType: null,
        reimburse: null,
        supplierId: null,
        supplierName: null,
        dateRange: "",
        params: {},
      },
      url: {
        list: '/goodsSalebuy/goodsSalebuy/list',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    // this.intDictsForPrj();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight);
  },
  methods: {
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    showsalebuyList(row) {
      this.queryParams.cgallid = row.id;
      this.open = true;
      this.getList();
    },
    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    getSummaries(param) {
      // console.log(param);
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "shengyu") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },


  }
};
</script>
